import instance from '../utils/request'
import qs from 'qs'
import { Message } from 'element-ui'

// User修改提交
async function editVisit (dormitory) {
  return await instance({
    url: 'visit',
    method: 'put',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    data: qs.stringify(dormitory)
  }).then(res => {
    return res
  }).catch(() => {
    Message.error('修改失败,服务器错误!')
  })
}

// User修改提交
async function addVisit (dormitory) {
  return await instance({
    url: 'visit',
    method: 'post',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    data: qs.stringify(dormitory)
  }).then(res => {
    return res
  }).catch(() => {
    Message.error('修改失败,服务器错误!')
  })
}

// 得到所有来访记录
async function getVisit (page, limit, search) {
  return await instance.get('/visit/getVisits?page=' + page + '&limit=' + limit + '&visitName=' + search).then(data => {
    return data
  }).catch(() => {
    Message.error('没有权限,请先登录')
  })
}

// 删除宿管
async function deleteVisit (visitId) {
  return await instance.delete('/visit/' + visitId).then(res => {
    return res
  }).catch(() => {
    Message.error('删除失败,服务器错误!')
  })
}

// 删除宿管
async function deleteAllVisit (visitId) {
  return await instance.delete('/visit/all/' + visitId).then(res => {
    return res
  }).catch(() => {
    Message.error('删除失败,服务器错误!')
  })
}

export default {
  editVisit,
  getVisit,
  deleteVisit,
  deleteAllVisit,
  addVisit
}
